03. Exercise: Visualize Distributions

Many variables tend to follow a Normal distribution (hence the name “Normal”), both in nature as well as artificial contexts. But there are other distributions as well, some that are variants of the Normal distribution, and some that are completely different! Each distribution is suitable for modeling certain kinds of variables.

In this exercise, you are given some samples of data. Plot the histogram of each sample, and then try to match it with the corresponding distribution.

Fill in the function plot_histogram with a line that plots a histogram of the data contained in the variable sample . Then write another line of code to show the plot. see documentation

Hint : check out the documentation here

Start Quiz:

"""Visualize the distribution of different samples."""

import pandas as pd
import matplotlib.pyplot as plt

def plot_histogram(sample, title, bins=16, **kwargs):
    """Plot the histogram of a given sample of random values.

    Parameters
    ----------
    sample : pandas.Series
        raw values to build histogram
    title : str
        plot title/header
    bins : int
        number of bins in the histogram
    kwargs : dict 
        any other keyword arguments for plotting (optional)
    """
    # TODO: Plot histogram
    
    # TODO: show the plot
    
    return


def test_run():
    """Test run plot_histogram() with different samples."""
    # Load and plot histograms of each sample
    # Note: Try plotting them one by one if it's taking too long
    A = pd.read_csv("A.csv", header=None, squeeze=True)
    plot_histogram(A, title="Sample A")
    
    B = pd.read_csv("B.csv", header=None, squeeze=True)
    plot_histogram(B, title="Sample B")
    
    C = pd.read_csv("C.csv", header=None, squeeze=True)
    plot_histogram(C, title="Sample C")
    
    D = pd.read_csv("D.csv", header=None, squeeze=True)
    plot_histogram(D, title="Sample D")


if __name__ == '__main__':
    test_run()

Match samples with distributions

QUIZ QUESTION: :

Listed below are some common distributions. Match the samples (A, B, C, D) with their corresponding distributions based on the histograms you plotted above.

You may refer to the figure below as a reference for what these distributions look like.

ANSWER CHOICES:



Sample

Distribution

Log-Normal

Exponential

Normal

Uniform

SOLUTION:

Sample

Distribution

Log-Normal

Exponential

Normal

Uniform

Probability Density Functions (PDFs) of common distributions

Probability Density Functions (PDFs) of common distributions